class Ngs < Formula
  desc "Powerful programming language and shell designed specifically for Ops"
  homepage "https://ngs-lang.org/"
  url "https://github.com/ngs-lang/ngs/archive/v0.2.9.tar.gz"
  sha256 "24ecf80a2fb99c2f3d24c125c22d38a73fb99ba4f9b99007c2b5a01c91906523"
  license "GPL-3.0"
  head "https://github.com/ngs-lang/ngs.git"

  bottle do
    cellar :any
    rebuild 1
    sha256 "c6b6e9a1c709cbac99dcbafe09153e361a136e4fff3a42ddb6e0d1c8f01ef7ed" => :big_sur
    sha256 "23c81230e132df4135226aad98f52efc9e3dbe0c3a5af6832f687278b92f7ace" => :catalina
    sha256 "ca696417660b80cb36e248021723b5ec200f5c986567b69eaaa4e063c00e8989" => :mojave
  end

  depends_on "cmake" => :build
  depends_on "pandoc" => :build
  depends_on "pkg-config" => :build
  depends_on "bdw-gc"
  depends_on "gnu-sed"
  depends_on "json-c"
  depends_on "pcre"
  depends_on "peg"

  uses_from_macos "libffi"

  # patch PR, https://github.com/ngs-lang/ngs/pull/382
  # remove in next release
  patch :DATA

  def install
    mkdir "build" do
      system "cmake", "..", *std_cmake_args
      system "make", "install"
    end
    share.install prefix/"man"
  end

  test do
    assert_match "Hello World!", shell_output("#{bin}/ngs -e 'echo(\"Hello World!\")'")
  end
end

__END__
diff --git a/build-scripts/bin2c.sh b/build-scripts/bin2c.sh
index 1020985..9ff327a 100755
--- a/build-scripts/bin2c.sh
+++ b/build-scripts/bin2c.sh
@@ -9,7 +9,7 @@ $file_name
 eof
 )
 len=$(wc -c < "$file_name")
-data=$(od -A n -t u1 -v "$file_name" | sed 's/ \+/,/g;1s/,//')
+data=$(od -A n -t u1 -v "$file_name" | gsed 's/ \+/,/g;1s/,//')
 printf '// Generated by %s from input file %s
 unsigned char %s[] = {
 %s
diff --git a/build-scripts/make-syntax-auto.sh b/build-scripts/make-syntax-auto.sh
index 8ed2db6..23bb3f2 100755
--- a/build-scripts/make-syntax-auto.sh
+++ b/build-scripts/make-syntax-auto.sh
@@ -2,6 +2,6 @@

 set -eu

-sed -n '/^typedef/p; /^struct/,/^$/p; /define YY_MAX_LINES/p;'
+gsed -n '/^typedef/p; /^struct/,/^$/p; /define YY_MAX_LINES/p;'
 echo 'int yyparse(yycontext *yyctx);'
 echo 'yycontext * yyrelease(yycontext *yyctx);'
diff --git a/build.macos.env.sh b/build.macos.env.sh
index dad8848..5b60927 100644
--- a/build.macos.env.sh
+++ b/build.macos.env.sh
@@ -1,4 +1,4 @@
-export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
+export PATH="/usr/local/opt/gnu-sed/libexec/bin:$PATH"
 pcp=$(dirname $(brew list pkg-config | grep '/bin/pkg-config'))
 export PATH="$pcp:$PATH"
 export PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig
